package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.Dept;
import com.lei.service.DeptService;
import com.lei.service.impl.DeptServiceImpl;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Lei Chu
 * @Date 2020/10/16 21:24
 */
@WebServlet("/deptServlet/*")
public class DeptServlet extends BaseServlet {

    private DeptService deptService = new DeptServiceImpl();

    /**
     * 功能描述:展示所有的部门信息
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String showAllDept(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Dept> page = new PageBean<>();
        TransBean.populate(page, request.getParameterMap());
        //判断查询是否带有条件
        if (page.getParams() != null) {
            if (isInt(page.getParams())) {
                //带的查询条件是个数字 通过ID模糊查找
                deptService.selectAllDeptById(page);
            } else {
                // 查询条件不是数字，则通过名字模糊查找
                deptService.selectAllDeptByName(page);
            }
        } else {
            // 未带查询条件，查询全部
            deptService.selectAllDept(page);
        }
        // 返回给页面数据
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:检测新增部门时，部门的名字是否可用
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String findDeptByNameToAdd(HttpServletRequest request, HttpServletResponse response) {
        String dname = request.getParameter("dname");
        int result = deptService.findIDDeptByName(dname);
        if (result != -1) {
            // 返回 0 表示该名字不可用
            return "0";
        }
        //返回 1 表示该名字可用
        return "1";
    }

    public String findDeptByNameToModify(HttpServletRequest request, HttpServletResponse response) {
        Dept dept = new Dept();
        TransBean.populate(dept, request.getParameterMap());
        System.out.println(dept);
        int result = deptService.findIDDeptByName(dept.getDname());
        System.out.println(result);
        if (result != -1 && result != dept.getDeptid()) {
            // 返回 0 表示该名字不可用
            return "0";
        }
        //返回 1 表示该名字可用
        return "1";
    }

    /**
     * 功能描述: 添加部门
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String addDept(HttpServletRequest request, HttpServletResponse response) {
        Dept dept = new Dept();
        TransBean.populate(dept, request.getParameterMap());
        return deptService.addDept(dept)+"";
    }

    /**
     * 功能描述:修改部门信息
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String updateDept(HttpServletRequest request,HttpServletResponse response){
        Dept dept = new Dept();
        TransBean.populate(dept, request.getParameterMap());
        return deptService.updateDept(dept)+"";
    }

    public String deleteDept(HttpServletRequest request,HttpServletResponse response){
        String deptid = request.getParameter("deptid");
        String result = deptService.deleteDept(Integer.parseInt(deptid));
        return result;
    }

    /**
     * 功能描述:获得所有的部门的id和name 用于添加员工的下拉框生成
     * @param request R
     * @param response R
     * @return java.lang.String
     */
    public String getAllDept(HttpServletRequest request,HttpServletResponse response){
        return JSON.toJSONString(deptService.getAllDept());
    }

    /**
     * 功能描述:查找最后的部门编号
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String showLastDeptNo(HttpServletRequest request, HttpServletResponse response) {
        int result = deptService.selectLastDeptNo();
        if (result != 0) {
            result++;
            return result + "";
        }
        return -1 + "";
    }

    /**
     * 功能描述:修改部门的状态
     * @param request Req
     * @param response Resp
     * @return java.lang.String
     */
    public String updateDeptStatus(HttpServletRequest request,HttpServletResponse response){
        String deptid = request.getParameter("deptid");
        String status = request.getParameter("status");
        int result = deptService.updateDeptStatus(Integer.parseInt(deptid),Integer.parseInt(status));
        return result+"";
    }
}
